package com.suning.sawp.service.impl.logistics;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.constants.LoggerConstants;
import com.suning.sawp.constants.LogisticsInfoConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.esb.OrderInfo;
import com.suning.sawp.dto.esb.OrderItemInfo;
import com.suning.sawp.dto.esb.QueryOrderPageRtn;
import com.suning.sawp.dto.logistics.OrderItem;
import com.suning.sawp.dto.logistics.LogisticsOrderList;
import com.suning.sawp.dto.logistics.OrderQueryRtn;
import com.suning.sawp.intf.logistics.OrderInfoService;
import com.suning.sawp.intf.logistics.QueryOrderPageService;
import com.suning.sawp.service.util.DJStringUtils;

/**
 * 
 * 物流信息接口实现类<br>
 * 〈功能详细描述〉
 * 
 * @author Wuxiao
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service
public class OrderInfoServiceImpl implements OrderInfoService {

    @Autowired
    QueryOrderPageService       queryOrderPageService;

    private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstants.SAWP_SERVICE);

    @Override
    public ReturnMsg<LogisticsOrderList> queryOrderListByPage(String storeCode,
            String oaAccountNo, String telNo, String startTime, String endTime, Integer start,
            Integer limit) {
        ReturnMsg<LogisticsOrderList> ret = new ReturnMsg<LogisticsOrderList>();
        // 校验入参
        if (!checkParams(oaAccountNo, storeCode, telNo, startTime, endTime, start, limit)) {
            LOGGER.info("调用OMS查询订单列表入参不合法,oaAccountNo:" + oaAccountNo + ",storeCode:" + storeCode
                    + ",telNo:" + telNo + ",startTime:" + startTime + ",endTime:" + endTime
                    + ",start:" + start + ",limit:" + limit);
            ret.setRetFlag(ReturnMsg.FAIL);
            ret.setErrorCode(ErrorCodeConstants.QUERY_ORDER_ERR_E001);
            ret.setErrorMessage(ErrorCodeConstants.QUERY_ORDER_ERR_MAP
                    .get(ErrorCodeConstants.QUERY_ORDER_ERR_E001));
            return ret;
        }
        // 调用OMS订单查询接口
        QueryOrderPageRtn response = queryOrderPageService.queryOrderInfoByPage(oaAccountNo,
                storeCode, telNo, startTime, endTime, start, limit);
        LogisticsOrderList orderList = new LogisticsOrderList();
        List<OrderQueryRtn> list = new ArrayList<OrderQueryRtn>();
        if (response != null && QueryOrderPageRtn.SUCCESS.equals(response.getOperateFlag())) {
            ret.setRetFlag(ReturnMsg.SUCCESS);
            if (CollectionUtils.isNotEmpty(response.getOrderLists())) {
                for (OrderInfo orderInfo : response.getOrderLists()) {
                    OrderQueryRtn order = new OrderQueryRtn();
                    order.setTelNo(DJStringUtils.sensitiveHandleEx(orderInfo.getMobilePhoneNum(),
                            3, 2));
                    order.setMemInCardNo(DJStringUtils.sensitiveHandle(orderInfo.getMemInCardNo()));
                    order.setOrderId(orderInfo.getOrderId());
                    order.setOrderTime(orderInfo.getOrderTime());
                    List<OrderItem> itemList = new ArrayList<OrderItem>();
                    if (CollectionUtils.isNotEmpty(orderInfo.getOrderbodyLists())) {
                        for (OrderItemInfo itemInfo : orderInfo.getOrderbodyLists()) {
                            OrderItem item = new OrderItem();
                            item.setCmmdtyCode(itemInfo.getCmmdtyCode());
                            item.setCmmdtyName(itemInfo.getCmmdtyName());
                            item.setItemStatus(LogisticsInfoConstants.ITEM_STATUS_MAP.get(itemInfo
                                    .getOrderItemStatus()));
                            item.setPosOrderId(itemInfo.getPosOrderId());
                            itemList.add(item);
                        }
                    }
                    order.setItemList(itemList);
                    list.add(order);
                }
            }
            orderList.setOrderList(list);
            ret.setData(orderList);
        } else if (response != null && QueryOrderPageRtn.FAIL.equals(response.getOperateFlag())) {
            if(response.getErrorMassage() != null && response.getErrorMassage().contains(LogisticsInfoConstants.NO_DATA_ERROR_MSG)){
                ret.setRetFlag(ReturnMsg.SUCCESS);
                orderList.setOrderList(list);
                ret.setData(orderList);
            }else{
                ret.setErrorCode(response.getErrorCode());
                ret.setErrorMessage(response.getErrorMassage());
            }
        } else {
            ret.setRetFlag(ReturnMsg.FAIL);
            ret.setErrorCode(ErrorCodeConstants.QUERY_ORDER_ERR_E999);
            ret.setErrorMessage(ErrorCodeConstants.QUERY_ORDER_ERR_MAP
                    .get(ErrorCodeConstants.QUERY_ORDER_ERR_E999));
        }
        return ret;
    }

    private boolean checkParams(String oaAccountNo, String storeCode, String telNo,
            String startTime, String endTime, Integer start, Integer limit) {
        if (StringUtils.isBlank(storeCode) || StringUtils.isBlank(startTime)
                || StringUtils.isBlank(endTime)) {
            return false;
        }
        if (start == null || limit == null || start < 0 || limit <= 0) {
            return false;
        }
        if (StringUtils.isBlank(oaAccountNo) && StringUtils.isBlank(telNo)) {
            return false;
        }
        return true;
    }

}
